package edu.fsu.cs.SHPE;

import java.net.ConnectException;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class VenueMapActivity extends MapActivity {

	private final static String TAG = "SHPE.VenueMapActivity";
	private MapView venueMap;
	private VenueContainer venueContainer;
	private long conferenceId;
	private List<Overlay> venueOverlays;
	private VenueItemizedOverlay venueItemizedOverlay;
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.venuemap);

		
		
		
		conferenceId = getIntent().getLongExtra("conferenceId", -1);

		Log.i(TAG, "conferenceId " + conferenceId);
		
		
		
		if(conferenceId == -1){
			Toast.makeText(this, "Error. No such conference id (" + conferenceId + ")", Toast.LENGTH_LONG).show();
			finish();
		}


		venueItemizedOverlay = new VenueItemizedOverlay(getResources().getDrawable(R.drawable.pin),this);
		venueMap = (MapView) findViewById(R.id.mapview);
		venueMap.setBuiltInZoomControls(true);
		try {
			venueContainer = new VenueContainer(Conference.insertId(getString(R.string.getVenus), conferenceId));
			
			venueOverlays = venueMap.getOverlays();

			for(Venue v : venueContainer){
				OverlayItem item = new OverlayItem(v.getGeoPoint(), v.getName(), v.getAddress());
				venueItemizedOverlay.addOverlay(item);
				venueMap.getController().setCenter(v.getGeoPoint());
			}

			venueOverlays.add(venueItemizedOverlay);
			
			
		} catch (ConnectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void onResume(){
		super.onResume();

		
		Intent intent = getIntent();
		
		Log.i(TAG, "onResume venueId " + intent.getLongExtra("venueId", -1));


		double lon,lat;

		


		if(intent.getLongExtra("venueId", -1) != -1){
			Venue venue = venueContainer.getVenueById(intent.getLongExtra("venueId", -1));
			lon = Double.parseDouble(venue.getLon());
			lat = Double.parseDouble(venue.getLat());
		}
		else{
			lon = intent.getDoubleExtra("lon", -1);
			lat = intent.getDoubleExtra("lat", -1);
		}

		GeoPoint point = new GeoPoint((int)(lat * 1000000), (int)(lon *  1000000));
		centerMap(point);
		venueMap.getController().setZoom(intent.getIntExtra("zoom", 0));

	} 

	private void centerMap(GeoPoint point){
		venueMap.getController().setCenter(point);
	}




	@Override
	protected boolean isRouteDisplayed() {

		return false;
	}



}
